﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using BrnMall.Core;
using BrnMall.Manager;
using BrnMall.Services;
using BrnMall.Web.Framework;
using BrnMall.Web.StoreAdmin.Models;

namespace BrnMall.Web.StoreAdmin.controllers
{
    /// <summary>
    /// 店铺后台商品预约控制器类
    /// </summary>
    public class ProductAppointController : BaseStoreAdminController
    {
        /// <summary>
        /// 商品预约列表
        /// </summary>
        public ActionResult ProductAppointList(string accountName, string productName, string consultMessage, string startTime, string endTime, int pid = -1,  int pageNumber = 1, int pageSize = 15)
        {
            int uid = AdminUsers.GetUidByAccountName(accountName);

            var page = ProductAppointManager.List(uid, pid, startTime, endTime, pageNumber, pageSize,WorkContext.StoreId);
            PageModel pageModel = new PageModel(pageSize, pageNumber, Convert.ToInt32(page.TotalItems));
            ProductAppointListModel model= new ProductAppointListModel()
            {
                Result = page,
                PageModel = pageModel,
                AccountName = accountName,
                Pid = pid,
                ProductName = string.IsNullOrWhiteSpace(productName) ? "选择商品" : productName,
              
                AppointStartTime = startTime,
                AppointEndTime = endTime
            };
            MallUtils.SetAdminRefererCookie(string.Format("{0}?pageNumber={1}&pageSize={2}&pid={3}&productName={4}&startTime={5}&endTime={6}&accountName={7}",
                Url.Action("ProductAppointList"),
                pageModel.PageNumber, pageModel.PageSize,
                pid, productName,
                startTime, endTime,
                 accountName));
            return View(model);
        }

        /// <summary>
        /// 更新商品预约状态
        /// </summary>
        /// <param name="consultId">商品咨询id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public ActionResult UpdateProductAppointState(int appointId = -1, int state = -1)
        {
            var appoint = ProductAppointManager.Get(appointId);
            if (appoint == null )
                return Content("0");

            bool result = ProductAppointManager.UpdateAppointStatus(appointId,state);
            if (result)
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }

        

        

        /// <summary>
        /// 删除商品预约
        /// </summary>
        /// <param name="consultIdList">商品咨询id列表</param>
        /// <returns></returns>
        public ActionResult DelProductAppoint(int[] appointIdList)
        {


            foreach (var id in appointIdList)
            {
                ProductAppointManager.DeleteAppoint(id);
            }
            
            return PromptView("商品预约删除成功");
        }
    }
}
